package pl.kosmowska.ewelina.client.app.activity;


import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.ClientFactory;
import pl.kosmowska.ewelina.client.app.activity.places.InvestmentEditPlace;
import pl.kosmowska.ewelina.client.app.activity.places.InvestmentPlace;
import pl.kosmowska.ewelina.client.app.activity.places.SignInPlace;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.InvestmentDataProxy;
import pl.kosmowska.ewelina.client.app.user.AuthEvent;
import pl.kosmowska.ewelina.client.app.user.AuthEventHandler;
import pl.kosmowska.ewelina.client.app.user.AuthEvent.Auth;
import pl.kosmowska.ewelina.client.views.InvestmentView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class InvestmentActivity extends AbstractActivity implements InvestmentView.Presenter {

	private InvestmentView view;

	private ClientFactory clientFactory;


	public InvestmentActivity(InvestmentPlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}


	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getInvestmentView();
		initAuthorisationForView();
		view.setPresenter(this);
		startView();
		containerWidget.setWidget(view.asWidget());

	}

	private void initAuthorisationForView() {
		if (view.isAlreadyInit() == false) {
			clientFactory.getEventBus().addHandler(AuthEvent.TYPE, new AuthEventHandler() {
				public void onAuthEvent(AuthEvent event) {
					Auth e = event.getAuthEvent();
					if (e == Auth.LOGGEDIN) {
						getInvestmentDataList();
					} else if (e == Auth.LOGGEDOUT) {
						goTo( new SignInPlace());
					}
				}
			});
		}
		view.setAlreadyInit(true);
	}

	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	private void startView() {
		if (clientFactory.getIsLoggedIn() == null) {
			// wait for login event b/c hasn't happened yet
		} else if (clientFactory.getIsLoggedIn() == true) {
			getInvestmentDataList();
		} else if (clientFactory.getIsLoggedIn() == false) { 
			goTo( new SignInPlace());
		}
	}

	public void getInvestmentDataList() {
		Request<List<InvestmentDataProxy>> req = clientFactory.getRequestFactory().getInvestmentDataRequest().findInvestmentDataByUser();
		req.fire(new Receiver<List<InvestmentDataProxy>>() {
			public void onSuccess(List<InvestmentDataProxy> investmentDataList) {
				view.reInitView(investmentDataList);
			}
			public void onFailure(ServerFailure error) {
				view.showMessage(ViewMessages.ERROR_TIME_OUT);
				super.onFailure(error);
			}
		});

	}

	@Override
	public void deleteInvestmentData(final InvestmentDataProxy selectedItem) {
		Request<Boolean> req = clientFactory.getRequestFactory().getInvestmentDataRequest().deleteInvestmentData(selectedItem.getId());
		req.fire(new Receiver<Boolean>() {
			public void onSuccess(Boolean data) {
				if (data != null && data.booleanValue() == true) {
					view.updateAfterInvestmentDataDelete(selectedItem);

				} else {
					view.showMessage(ViewMessages.ERROR_ON_DELETE);
				}
			}
			public void onFailure(ServerFailure error) {
				view.showMessage(ViewMessages.ERROR_ON_DELETE);
				super.onFailure(error);
			}
		});

	}


	@Override
	public void goToInvestmentAddPlace() {
		goTo( new InvestmentEditPlace() );

	}


	@Override
	public void goToInvestmentEditPlace(InvestmentDataProxy selectedItem) {
		goTo( new InvestmentEditPlace(selectedItem) );

	}

}


